package cn.tsvtc.shop.controller;

import cn.hutool.core.util.IdUtil;
import cn.tsvtc.shop.entity.LoginUser;
import cn.tsvtc.shop.entity.ResultMessage;
import cn.tsvtc.shop.service.CartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: Athena
 * @date: 2020/11/3 20:32
 * @description:
 */
@RestController
@RequestMapping("cart")
@Slf4j
public class CartController {

    private RestTemplate restTemplate;

    private CartService cartService;

    @Autowired
    public CartController(CartService cartService,RestTemplate restTemplate){
        this.cartService=cartService;
        this.restTemplate=restTemplate;
    }


    @RequestMapping("show")
    public ResultMessage show(@CookieValue(value = "cart_token",required = false) String cart_token,
                              @CookieValue(name = "login_token", required = false) String login_token){

        if (!StringUtils.isEmpty(login_token)) {
            LoginUser userByLoginToken = cartService.getUserByLoginToken(login_token);
            ResultMessage message =null;
            if(userByLoginToken!=null){
                //有对应的用户
                message = cartService.show(userByLoginToken.getId().toString());
            }else{
                message = cartService.show(login_token);
            }
            return message;
        }else if(!StringUtils.isEmpty(cart_token)){
            ResultMessage message = cartService.show(cart_token);
            return message;
        }else{
            //空token
            return new ResultMessage(false,"failed","empty cookie params");
        }

    }


    @GetMapping("merge")
    public ResultMessage merge(@CookieValue(name = "cart_token", required = false) String cart_token,
                               @CookieValue(name = "login_token", required = false) String login_token,
                               HttpServletResponse response){
        //先判断是否登录,以及是否同时拥有cart_token和login_token
        boolean cart_token_flag = StringUtils.isEmpty(cart_token);
        boolean login_token_flag = StringUtils.isEmpty(login_token);
        if(!login_token_flag){
            //已经登录
            //通过login_token获取user的对象
            LoginUser userByLoginToken = cartService.getUserByLoginToken(login_token);
            if (userByLoginToken == null) {
                //没有找对应login_token的user
                return new ResultMessage(false,"failed","login_token error");
            }else{
                //检查是否需要合并
                ResultMessage mergeAfterMessage = null;
                if (!cart_token_flag) {
                    //需要合并
                    mergeAfterMessage = cartService.merge(cart_token, userByLoginToken.getId().toString());
                    //删除cart_token cookie
                    Cookie newcookie = new Cookie("cart_token", null);
                    newcookie.setPath("/");
                    newcookie.setMaxAge(0);
                    response.addCookie(newcookie);
                    return mergeAfterMessage;
                } else {
                    //不需要合并，直接创建新的
                    //ResultMessage add = cartService.add(pid, num, userByLoginToken.getId().toString());
                    Cookie newcookie = new Cookie("cart_token", null);
                    newcookie.setPath("/");
                    newcookie.setMaxAge(0);
                    response.addCookie(newcookie);
                    return new ResultMessage(false,"failed","不需要合并");
                }
            }
        }else{
            //登录信息有误
            Cookie newcookie = new Cookie("cart_token", null);
            newcookie.setPath("/");
            newcookie.setMaxAge(0);
            response.addCookie(newcookie);
            return new ResultMessage(false,"failed","没有登录信息，不能合并");
        }

    }


    @GetMapping("add")
    public ResultMessage add(@RequestParam(value = "pid",required = true)Long pid,
                             @RequestParam(value = "num",required = true)Integer num,
                             @CookieValue(value = "cart_token",required = false) String cart_token,
                             @CookieValue(value = "login_token",required = false) String login_token,
                             HttpServletResponse response){

        //先判断是否登录,以及是否同时拥有cart_token和login_token
        boolean cart_token_flag = StringUtils.isEmpty(cart_token);
        boolean login_token_flag = StringUtils.isEmpty(login_token);
        if(!login_token_flag){
            //已经登录
            //通过login_token获取user的对象
            LoginUser userByLoginToken = cartService.getUserByLoginToken(login_token);
            if (userByLoginToken == null) {
                //没有找对应login_token的user
                return new ResultMessage(false,"failed","login_token error");
            }else{


                //不需要检查合并问题，直接作为新用户存储，只在登录时检查合并问题
                ResultMessage add = cartService.add(pid, num, userByLoginToken.getId().toString());
                return add;
            }
        }else {
            //没有登录
            //先判断有没有cart_token，有则是已经添加过cart了，没有则是匿名用户且未访问过购物车，需要生成新的uuid的cart_token
            String cartToken=null;
            if (cart_token != null) {
                cartToken=cart_token;
            }else{
                //给新用户随机uuid,用于cart保存
                cartToken= IdUtil.simpleUUID();
                Cookie newcookie=new Cookie("cart_token",cartToken);
                newcookie.setPath("/");
                newcookie.setMaxAge(60*60*24);
                response.addCookie(newcookie);
                log.error("conroller add 设置了新的cookie===> "+cartToken+"   此时cart_token_flag===>"+cart_token_flag+"   此时login_token_flag===>"+login_token_flag);
            }
            ResultMessage add = cartService.add(pid, num, cartToken);
            return add;
        }



    }

}
